//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;
using System.Linq.Expressions;
using System.Xml.Serialization;

namespace SFWK.Core.ExpressionBuilder
{
    [DataContract]
    [KnownType(typeof(EditableMemberAssignment))]
    [KnownType(typeof(EditableMemberListBinding))]
    [KnownType(typeof(EditableMemberMemberBinding))]
    [XmlInclude(typeof(EditableMemberAssignment))]
    [XmlInclude(typeof(EditableMemberListBinding))]
    [XmlInclude(typeof(EditableMemberMemberBinding))]
    public abstract class EditableMemberBinding
    {       
        // Properties
        public abstract MemberBindingType BindingType
        {
            get;
            set;
        }

        [XmlIgnore]
        public MemberInfo Member
        {
            get;
            set;
        }

        [DataMember]
        public string MemberName
        {
            get { return Member.ToSerializableForm(); }
            set { Member = Member.FromSerializableForm(value); }
        }

        // Ctors
        protected EditableMemberBinding()
        {
        }

        protected EditableMemberBinding(MemberBindingType type, MemberInfo member)
        {
            BindingType = type;
            Member = member;
        }

        // Methods
        public abstract MemberBinding ToMemberBinding();

        public static EditableMemberBinding CreateEditableMemberBinding(MemberBinding member)
        {
            if (member is MemberAssignment) return new EditableMemberAssignment(member as MemberAssignment);
            else if (member is MemberListBinding) return new EditableMemberListBinding(member as MemberListBinding);
            else if (member is MemberMemberBinding) return new EditableMemberMemberBinding(member as MemberMemberBinding);
            else return null;

        }
    }
}

